package com.wxxymaker.service.impl;

import com.blade.kit.EncrypKit;
import com.wxxymaker.model.dao.AdminDao;
import com.wxxymaker.model.entity.Admin;
import com.wxxymaker.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.Date;
import java.util.List;

@Service("adminService")
public class AdminServiceImpl implements AdminService {

    private final AdminDao dao;

    @Autowired
    public AdminServiceImpl(AdminDao dao) {
        this.dao = dao;
    }

    @Override
    @Transactional
    public Admin add(Admin admin) {
        admin.setPassword(EncrypKit.md5(admin.getPassword()));
        return dao.save(admin);
    }

    @Override
    @Transactional
    public Admin delete(Integer id) {
        Admin admin = dao.findOne(id);
        // 只有普通管理能被删除
        if (null != admin && admin.getGrade() == Admin.gradeDim.COMMON){
            dao.delete(admin);
        }
        return admin;
    }

    @Override
    @Transactional
    public boolean change(Admin admin) {
        Integer id = admin.getId();
        if (null != id && id > 0) {
            if (!StringUtils.isEmpty(admin.getPassword())) {
                admin.setPassword(EncrypKit.md5(admin.getPassword()));
            }
            Admin one = (Admin) dao.findOne(id).extend(admin);
            return dao.saveAndFlush(one) != null;
        }
        return false;
    }

    @Override
    public Admin getOne(Integer id) {
        return dao.findOne(id);
    }

    @Override
    public Page<Admin> page(Integer pageNo, Integer size, Integer grade, String name) {
        Specification<Admin> specification = new Specification<Admin>() {
            @Override
            public Predicate toPredicate(Root<Admin> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                if (grade != null && grade > 0) {
                    query.where(cb.equal(root.get("grade"), grade));
                }
                if (!"".equals(name)) {
                    query.where(cb.like(root.get("name"), "%" + name + "%"));
                }
                return null;
            }
        };
        Pageable pageable = new PageRequest(pageNo - 1, size,
                new Sort(Sort.Direction.ASC, "id"));
        return dao.findAll(specification, pageable);
    }

    @Override
    public List<Admin> getByName(String name) {
        return dao.findByNameLike(name);
    }

    @Override
    public boolean checkAdmin(Admin admin) {
        Admin a = dao.findByUsernameAndPassword(admin.getUsername(),
                EncrypKit.md5(admin.getPassword()));
        if (null != a && a.getId() > 0) {
            a.setLoginTime(new Date());
            dao.saveAndFlush(a);
            return true;
        }
        return false;
    }

    @Override
    public Admin getByUsername(String username) {
        return dao.findByUsername(username);
    }
}
